package com.liqi.frame.comm.service.impl;

import cn.hutool.core.util.StrUtil;
import com.liqi.frame.comm.base.BaseDao;
import com.liqi.frame.comm.base.BaseServiceImpl;
import com.liqi.frame.comm.dao.UserOnlineDao;
import com.liqi.frame.comm.po.UserOnline;
import com.liqi.frame.comm.service.UserOnlineService;
import com.liqi.frame.comm.session.OnlineSession;
import org.apache.shiro.session.Session;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Objects;

/**
 * @author liqi on 2020/1/20 0020. liqiwork@qq.com
 */
@Service("userOnlineService")
public class UserOnlineServiceImpl extends BaseServiceImpl<UserOnline> implements UserOnlineService {
    @Autowired
    private UserOnlineDao userOnlineDao;

    @Override
    protected BaseDao getDao() {
        return userOnlineDao;
    }

    @Override
    public int saveOnline(UserOnline userOnline) {
        return userOnlineDao.insertObject(
                userOnline.getId(),
                userOnline.getGroupName(),
                userOnline.getFullname(),
                userOnline.getIpaddr(),
                userOnline.getLoginLocation(),
                userOnline.getBrowser(),
                userOnline.getOs(),
                userOnline.getStartTime(),
                userOnline.getLastAccessTime(),
                userOnline.getExpireTime(),
                userOnline.getStatus()
                );
    }

    @Override
    public int deleteSession(OnlineSession onlineSession) {
        return userOnlineDao.deleteObjectById(onlineSession.getId()+"");
    }

    @Override
    public Session getSession(String sessionId) {
        UserOnline userOnline = userOnlineDao.getObjectById(sessionId);
        OnlineSession onlineSession = new OnlineSession();
        if (!Objects.isNull(userOnline)){
            onlineSession.setId(userOnline.getId());
            onlineSession.setHost(userOnline.getIpaddr());
            onlineSession.setBrowser(userOnline.getBrowser());
            onlineSession.setOs(userOnline.getOs());
            onlineSession.setGroupName(userOnline.getGroupName());
            onlineSession.setLoginName(userOnline.getFullname());
            onlineSession.setStartTimestamp(userOnline.getStartTime());
            onlineSession.setLastAccessTime(userOnline.getLastAccessTime());
            onlineSession.setTimeout(userOnline.getExpireTime());
            return onlineSession;
        } else {
            return null;
        }
    }

    @Override
    public List<UserOnline> getListWithFullname(String fullname) {
        return userOnlineDao.getListWithFullname(fullname);
    }
}
